package com.example.administrator.wallet.view.fragment.quotation;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.CoinTransactionActivityListviewAdapter;
import com.example.administrator.wallet.base.BaseFragment;
import com.example.administrator.wallet.custom.MessageEvent;
import com.example.administrator.wallet.model.bean.BTCFragmentDataBean;
import com.example.administrator.wallet.model.bean.CoinMarketBean;
import com.example.administrator.wallet.model.bean.WebSocketPriceBean;
import com.example.administrator.wallet.model.bean.WebSocketTodayMarketBean;
import com.example.administrator.wallet.view.activity.find.cointrans.CoinTransactionActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

import static android.support.test.InstrumentationRegistry.getArguments;

/**
 * A simple {@link Fragment} subclass.
 */
public class BTCFragment extends BaseFragment {

    @Bind(R.id.lv_fragment_btc)
    ListView listView;
    private CoinTransactionActivityListviewAdapter adapter;
    private List<WebSocketPriceBean> priceList;
    private List<WebSocketTodayMarketBean> todayMarketList;
    private List<BTCFragmentDataBean> dataBeanList;
    CoinTransactionActivity activity;

    @Override
    public void initOnCreate(){
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        activity = (CoinTransactionActivity) getActivity();

        Bundle bundle = getArguments();
        CoinMarketBean.DataBean dataBean = (CoinMarketBean.DataBean) bundle.getSerializable("detailList");
        final List<CoinMarketBean.DataBean.ListBean> list =  dataBean.getList();

        priceList = new ArrayList<>();
        todayMarketList = new ArrayList<>();
        dataBeanList = new ArrayList<>();

        adapter = new CoinTransactionActivityListviewAdapter(activity,dataBeanList);
        listView.setAdapter(adapter);

        JSONArray subscribeArray = new JSONArray();
        for (CoinMarketBean.DataBean.ListBean data : list) {
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(data.getName());
            subscribeArray.add(data.getName());
            int getPriceId = activity.sendMeessage("price.query",jsonArray);
            WebSocketPriceBean price = new WebSocketPriceBean(getPriceId,data.getName(),"");
            priceList.add(price);

            int getTodayMarketId = activity.sendMeessage("today.query",jsonArray);
            WebSocketTodayMarketBean todayMarketBean = new WebSocketTodayMarketBean(getTodayMarketId,data.getName(),"","");
            todayMarketList.add(todayMarketBean);

            BTCFragmentDataBean fragmentDataBean = new BTCFragmentDataBean(data.getName(),"","",0.0,data);
            dataBeanList.add(fragmentDataBean);
        }

        activity.sendMeessage("price.subscribe",subscribeArray);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent){

        JSONObject jsonObject = (JSONObject) JSONObject.parse(messageEvent.getMessage());

        if (jsonObject.get("id") != null){
            for (WebSocketPriceBean price : priceList) {
                if (jsonObject.getInteger("id") == price.getId()){
                    if (jsonObject.get("result") != null) {
                        price.setPrice(jsonObject.getString("result"));
                    }
                }

                for (BTCFragmentDataBean dataBean:dataBeanList) {

                    if (price.getMarket().equals(dataBean.getMarket())){
                        dataBean.setPrice(price.getPrice());
                    }
                }
            }

            for (WebSocketTodayMarketBean today : todayMarketList) {
                if (jsonObject.getInteger("id") == today.getId()){
                    if (jsonObject.get("result") != null){
                        JSONObject json = jsonObject.getJSONObject("result");
                        today.setOpen(json.getString("open"));
                        today.setLast(json.getString("last"));
                        today.setUpsOrDowns();
                    }
                }

                for (BTCFragmentDataBean dataBean:dataBeanList) {
                    if (today.getMarket().equals(dataBean.getMarket())){
                        dataBean.setUpsOrDowns(today.getUpsOrDowns());
                    }
                }
            }
        } else {
            if (jsonObject.getString("method").equals("price.update")){
                for (WebSocketPriceBean priceBean : priceList){
                    JSONArray priceArray = jsonObject.getJSONArray("params");
                    if (priceBean.getMarket().equals(priceArray.get(0))){
                        priceBean.setPrice(priceArray.getString(1));
                    }
                }
            }
        }

        adapter.notifyDataSetChanged();
    }

    @Override
    public void init() {

    }

    @Override
    public int getLayoutResource() {
        return R.layout.fragment_btc;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
